;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Enterprise.ServiceStaffQuota", [])
        .controller("ServiceStaffQuotaCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter, UserContextService) {
            $scope.activeFlag = [true, false, false];
            $scope.markInfo = {
                allChecked: false
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.submitted = false;
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.condition = {};
            $scope.roleList = [];
            $scope.dataList = [];
            $scope.detailInfo = {
                sex: 1,
                locked: 0
            };
            $scope.dataSelected = {
                role: {},
                department: {}
            };
            $scope.userStatues = [
                {
                    id: 0,
                    name: "禁用"
                }, {
                    id: 1,
                    name: "启用"
                }
            ];
            $scope.tabFlag = {};
            var tabFlag = {
                0: "新增",
                1: "编辑"
            };

            $scope.tableConfig = {
                scrollWidth: 1250,
                fixedWidth: 240,
                tableCol: [
                    {
                        type: "",
                        name: "服务人员账号",
                        dataField: "staffAccount",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "服务人员名称",
                        dataField: "staffName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "借用额度",
                        dataField: "borrowingQuota",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "已借用金额",
                        dataField: "borrowedAmount",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "欠款额度",
                        dataField: "arrears",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }
                    ,
                    {
                        type: "",
                        name: "未收回款金额",
                        dataField: "pendingPayment",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },{
                        type: "",
                        name: "状态",
                        dataField: "statusShow",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },{
                        type: "",
                        name: "备注",
                        dataField: "remark",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 200,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };

            var btnPermissionCode = {
                serviceStaffQuotaEdit: window.ActionCode.serviceStaffQuotaEdit,
                serviceStaffQuotaEnable: window.ActionCode.serviceStaffQuotaEnable,
            };

            $scope.btnPermission = {
                serviceStaffQuotaEdit: true,
                serviceStaffQuotaEnable: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {scrollLeft: {left: $scope.tableConfig.fixedWidth + "px"}};
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    default:
                        break;
                }
            };

            function initData() {
                $scope.tabFlag = {};
                $scope.detailInfo = {
                    sex: 1,
                    locked: 0
                };
                $scope.roleList = [];
                $scope.dataSelected = {
                    role: {},
                    department: {}
                };
            }

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/serviceStaffQuota/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.statusShow = $filter("TrueOrFalse")(item.status, "enabled");
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.selectAll = function () {
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
            };

            function checkAll() {
                var selectAll = false;
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!$scope.dataList[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    if (!checkAll()) {
                        $scope.markInfo.allChecked = false;
                    } else {
                        $scope.markInfo.allChecked = true;
                    }
                }
            };

            $scope.statusChange = function (operateType, statusType,rowId) {
                var service = "device";
                var reqUrl = "/serviceStaffQuota/batchEnable";
                if (statusType === 0) {
                    reqUrl = "/serviceStaffQuota/batchDisable";
                }
                var param = {};
                param.listId = [];

                if (operateType === 0) {
                    param.listId.push(rowId);
                }

                if (operateType === 1) {
                    for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                        if (!!$scope.dataList[i].isChecked&& !!$scope.dataList[i].id) {
                            param.listId.push($scope.dataList[i].id);
                        }
                    }
                }
                if (param.listId.length === 0) {
                    ybWidget.ShowMessage('请选择需要操作的行');
                    return;
                }
                var tipMsg = "禁用";
                if (statusType === 1) {
                    tipMsg = "启用";
                }
                ybWidget.ShowMessage("确定要"+tipMsg+"吗？", MESG_TYPE.Ask).then(function () {
                    $scope.markInfo.allChecked = false;
                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    })
                }, function () {

                })
            };

            $scope.editData = function (id) {
                var service = "device";
                var reqUrl = "/serviceStaffQuota/item";
                var param = {};
                param.userId = id;

                ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.tabFlag.type = 1;
                    $scope.tabFlag.name = tabFlag[1];
                    $scope.changeTab(1);
                    // $scope.dataSelected.role.list = resp.value.roleIds;
                    // $scope.dataSelected.role.name = resp.value.roleNames;
                    // $scope.dataSelected.department.id = resp.value.deptId;
                    // $scope.dataSelected.department.departName = resp.value.deptName;
                    $scope.detailInfo = resp.value;
                    if(!resp.value.borrowedAmount){
                        resp.value.borrowedAmount=0;
                    }
                    if(!resp.value.pendingPayment){
                        resp.value.pendingPayment=0;
                    }
                    $scope.detailInfo.surplusQuota =resp.value.borrowingQuota-resp.value.borrowedAmount;
                    $scope.detailInfo.surplusArrears =resp.value.arrears-resp.value.pendingPayment;
                });
            };

            $scope.saveData = function (isValid) {
                if (!isValid ) {
                    $scope.submitted = true;
                    return;
                }
                var service = "device";
                var reqUrl = "/serviceStaffQuota/save";
                if ($scope.tabFlag.type === 1) {
                    reqUrl = "/serviceStaffQuota/save";
                }
                ybHttp.Post(service, reqUrl, $scope.detailInfo).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    getDataList();
                    $scope.markInfo.allChecked = false;
                });
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);
